package com.zcc.lesson04.utils;

import com.zcc.lesson02.utils.JDBCUtils;
import org.apache.commons.dbcp.BasicDataSourceFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class DBCPUtils {
    static DataSource dataSource;
    static {
        try {
            //获取配置文件的数据流
            InputStream inputStream = JDBCUtils.class.getClassLoader().getResourceAsStream("dbcp.properties");
            //新建properties对象
            Properties properties = new Properties();
            //读取数据流
            properties.load(inputStream);

            //通过Factory创建DataSource对象
            dataSource = BasicDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    //获取连接
    //通过DataSource对象的getConnection方法
    //用return获得返回值
    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }
    //释放连接
    public static void release(Connection connection, Statement statement, ResultSet resultSet) {
        JDBCUtils.release(connection, statement, resultSet);
    }
}
